package com.tl.sboot.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tl.sboot.entity.Parameter;
import com.tl.sboot.mapper.StkCodeMapper;
import com.tl.sboot.service.StkCodeService;
import com.tl.sboot.utils.RedisUtil;
import com.tl.sboot.utils.StkSqlUtil;
@Service
public class StkCodeServiceImpl implements StkCodeService{
	@Autowired
	StkCodeMapper stkCodeMapper;
	@Autowired
	RedisUtil ru;
	@Override
	public List<Map<String, Object>> findByParStkCode(String parListJson) {
		String tableName = "STK_COM_DIST_CHNG";
		List<Parameter> parameters = new ArrayList<Parameter>();
		Parameter parameter = new Parameter();
		parameter.setFieldName("CName");
		parameter.setCondition("=");
		parameter.setValue("'北京中人网信息咨询股份有限公司'");
		parameters.add(parameter);
		List<Map<String, Object>> findByParStkCode = stkCodeMapper.findByParStkCode(parameters, tableName);
		System.out.println(findByParStkCode);
		return findByParStkCode;
	}
	/**
	 * 
	 * @return
	 */
	public List<Map<String, Object>> findFieldsWithObjList(String param){
		param = param.substring(1);
		//判断是否选择策略
		int DKBF = param.indexOf("~DKBF");
		if (DKBF!=-1) {
			param = param.substring(0,DKBF)+param.substring(DKBF+5);
		}
		int BKBF = param.indexOf("~BKBF");
		if (BKBF!=-1) {
			param = param.substring(0,BKBF)+param.substring(BKBF+5);
		}
		int MACD = param.indexOf("~MACD");
		if (MACD!=-1) {
			param = param.substring(0,MACD)+param.substring(MACD+5);
		}
		//判断是否选择了limit
		int limit = param.indexOf("limit:");
		int top = 0;
		if (limit!=-1) {
			top = Integer.parseInt(param.substring(limit+6));
			param = param.substring(0,limit);
		}else{
			top = 10;
		}
		//判断是否选择了排序
		String sort = "DESC";
		int DESC = param.indexOf("~DESC");
		int ASC = param.indexOf("~ASC");
		if (DESC!=-1) {
			param = param.substring(0,DESC)+param.substring(DESC+5);
		}
		if (ASC!=-1) {
			sort = "ASC";
			param = param.substring(0,ASC)+param.substring(ASC+4);
		}
		
		//判断是否选择了区间
		String minAndMax = "";
		int min = param.indexOf("min:");
		if (min!=-1) {
			int max = param.indexOf(":max");
			minAndMax = param.substring(min+4,max);
			param = param.substring(0,min)+param.substring(max+4);
		}
		StkSqlUtil sqlUtil = new StkSqlUtil(sort, 0,top,minAndMax);
    	String sql = sqlUtil.getSql(param);
    	List<Map<String, Object>> maps = stkCodeMapper.findFieldsWithObjList(sql);//数据库中数据
    	if (!maps.isEmpty()) {
    		if (DKBF!=-1) {
        		maps = retuenRedisMap("DKBF",maps);
    		}
        	if (BKBF!=-1) {
        		maps = retuenRedisMap("BKBF",maps);
    		}
        	if (MACD!=-1) {
        		maps = retuenRedisMap("MACD",maps);;
    		}
		}
    	
		return maps;
	}
	/**
	 * 获取相同的stock
	 * @param strategy
	 * @param maps
	 * @return
	 */
	public List<Map<String, Object>> retuenRedisMap(String strategy,List<Map<String, Object>> maps){
		List<Map<String, Object>> redisMap = new ArrayList<>();
		Set<String> zrange = ru.zrangeByScore("SS.QUANT."+strategy+".20161124.SSE.A_STOCK", 9000,0);
		for (Map<String, Object> map : maps) {
			for (String stock : zrange) {
				if (map.get("STOCKCODE").equals(stock)) {
					redisMap.add(map);
				}
			}
		}
		return redisMap;
	}
	
}
